<?php

include 'api.php';
include 'connect_pdo.php';
include 'classes.php';
require 'loginoptional.php'; 
$pdo = new pdo_query;

$realm = $_GET['realm'];
$char = $_GET['char'];
$realm = stripslashes($realm);

try{
	$data = $api->getCharacterApi()->getCharacter($realm, $char, array('achievements'));

}
catch(exception $e)
{echo $e;}

$points = $data['achievementPoints'];
$achComplete = $data['achievements']['achievementsCompleted'];
$achCompleteTime = $data['achievements']['achievementsCompletedTimestamp'];

$achievementsCompleted = array_combine($achComplete, $achCompleteTime);
arsort($achievementsCompleted);

$sqlGetAch = "SELECT title, description, points FROM achievement WHERE achievement_id = ?";

//output
echo "Achievements earned by $char on $realm (total $points points)<br/><br/>";

foreach ($achievementsCompleted as $ach=>$time)
{
	
	$pdo->bound_query($sqlGetAch,array($ach),'One');
	$result = $pdo->result;
	/*
$STH = $DBH->prepare($sqlGetAch);
	$STH->bindParam(1, $ach);
	$STH->execute();
	$result = $STH->fetch();
*/
	$time = str_ireplace("'","",$time);
	$time = substr($time,0,10);
	
	if (is_numeric($time))
	{
	$date = date('r',$time);
	}
	
	if (!empty($result))
		{echo "<a target='_blank' href='http://www.wowhead.com/achievement=$ach' rel='achievement=$ach'>",$result[0],"</a> - ",$result[2]," points on $date<br/>";}
	else 
		{echo $ach,"<br/>";}
}


include 'footer.html';





?>